package com.projetosi.web.client;

/* 
 * Ext GWT 2.2.5 - Ext for GWT 
 * Copyright(c) 2007-2010, Ext JS, LLC. 
 * licensing@extjs.com 
 *  
 * http://extjs.com/license 
 */  

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.charts.client.model.axis.Label;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.DataProxy;
import com.extjs.gxt.ui.client.data.DataReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.projetosi.web.server.sistema.objetos.Item;

public class TelaInicial extends LayoutContainer {  
	ContentPanel center;
	TabPanel tabItem, tabInfo;
	FormPanel visualizarMeusAmigosPanel;
	protected void onRender(Element target, int index) {  
		super.onRender(target, index);  
		final BorderLayout layout = new BorderLayout();  
		setLayout(layout);  
		setStyleAttribute("padding", "0px");  
		Button botaoLogOut = new Button("Log Out", new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				new ProjetoSI_WEB().onModuleLoad();

			}
		});
		ListView<Label> labels = new ListView<Label>();

		labels.getSelectionModel().addListener(Events.SelectionChange, new Listener<SelectionChangedEvent<Label>>() {  
			public void handleEvent(SelectionChangedEvent<Label> be) {  
				onSelectionChange(be);  
			}  
		});

		ListStore<Label> storeLabels = new ListStore<Label>();
		storeLabels.add(new Label("Perfil"));
		storeLabels.add(new Label("Amigos"));
		storeLabels.add(new Label("Itens"));
		storeLabels.add(new Label("Mensagens"));
		labels.setStore(storeLabels);
		labels.setBorders(false);
		ContentPanel north = new ContentPanel();  
		ContentPanel west = new ContentPanel();  
		center= new ContentPanel();
		tabItem = new TabPanel();
		tabInfo = new TabPanel();
		tabInfo.setSize(400,500);
		tabInfo.setAutoWidth(true);
		tabInfo.setPlain(true);
		tabItem.setSize(400,500);
		tabItem.setAutoWidth(true);
		tabItem.setPlain(true);
		TabItem cadastrarItemTxt = new TabItem("Cadastrar Item");
		cadastrarItemTxt.addStyleName("pad-text");  
		TabItem visualizarMeusItensTxt = new TabItem("Meus itens");
		visualizarMeusItensTxt.addStyleName("pad-text");
		TabItem informacoesPessoaisTxt =  new TabItem("Informacoes Pessoais");
		FormPanel cadastrarItemPanel = criarPanel();
		cadastrarItemPanel.setWidth(280);
		FormPanel visualizarMeusItensPanel = criarPanel();
		visualizarMeusItensPanel.setWidth(280);
		FormPanel informacoesPessoaisPanel = criarPanel();
		informacoesPessoaisPanel.setWidth(280);
		visualizarMeusAmigosPanel = criarPanel();
		visualizarMeusAmigosPanel.setWidth(950);
		TextField<String> nomeItemField = new TextField<String>();
		nomeItemField.setFieldLabel("Nome");
		nomeItemField.setAllowBlank(false);
		TextField<String> descricaoItemField = new TextField<String>();
		descricaoItemField.setFieldLabel("Descricao");
		descricaoItemField.setAllowBlank(false);
		TextField<String> categoriaItemField = new TextField<String>();
		categoriaItemField.setFieldLabel("Categoria");
		categoriaItemField.setAllowBlank(false);

		cadastrarItemPanel.add(nomeItemField);
		cadastrarItemPanel.add(descricaoItemField);
		cadastrarItemPanel.add(categoriaItemField);
		
		visualizarMeusItensPanel.add(visualizarMeustIntesLayout());
		visualizarMeusAmigosPanel.add(visualizarAmigosLayout());

		cadastrarItemPanel.addButton(new Button("Confirmar", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				//TODO logica a ser implementada
				MessageBox message = new MessageBox();
				message.setMessage("Cadastro OK!");
				message.show();

			}
		}));		



		cadastrarItemTxt.add(cadastrarItemPanel);
		visualizarMeusItensTxt.add(visualizarMeusItensPanel);
		
		tabItem.add(cadastrarItemTxt);
		tabItem.add(visualizarMeusItensTxt);
		tabInfo.add(informacoesPessoaisTxt);
		center.add(tabInfo);
		center.add(tabItem);
		center.add(visualizarMeusAmigosPanel);
		visualizarMeusAmigosPanel.hide();
		tabItem.hide();
		tabInfo.hide();
		ContentPanel east = new ContentPanel();  
		//ContentPanel south = new ContentPanel();  

		//center.setHeading();  
		center.setScrollMode(Scroll.AUTOX);    

		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, 100);  
		northData.setCollapsible(true);  
		northData.setFloatable(true);  
		northData.setHideCollapseTool(true);  
		northData.setSplit(true);  
		northData.setMargins(new Margins(0, 0, 5, 0));  

		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 200);  
		westData.setSplit(true);  
		westData.setCollapsible(true);  
		westData.setMargins(new Margins(0,5,0,0));  

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
		centerData.setMargins(new Margins(0));  

		BorderLayoutData eastData = new BorderLayoutData(LayoutRegion.EAST, 200);  
		eastData.setSplit(true);  
		eastData.setCollapsible(true);  
		eastData.setMargins(new Margins(0,0,0,5));  

		BorderLayoutData southData = new BorderLayoutData(LayoutRegion.SOUTH, 100);  
		southData.setSplit(true);  
		southData.setCollapsible(true);  
		southData.setFloatable(true);  
		southData.setMargins(new Margins(5, 0, 0, 0));  

		west.add(labels);
		north.add(botaoLogOut);

		add(north, northData);  
		add(west, westData);  
		add(center, centerData);  
		add(east, eastData);  
		//add(south, southData);  
	}  

	private void onSelectionChange(SelectionChangedEvent<Label> le) {  
		if (le.getSelection().size() > 0) {  
			if(le.getSelectedItem().getText().equals("Itens")){
				itensClicado();

			}else if(le.getSelectedItem().getText().equals("Amigos")){
				tabItem.hide();
				tabInfo.hide();
				visualizarMeusAmigosPanel.show();
			}else if(le.getSelectedItem().getText().equals("Perfil")){
				tabItem.hide();
				visualizarMeusAmigosPanel.hide();
				tabInfo.show();
			}
		}
	}

	private void itensClicado(){
		tabItem.show();
		tabInfo.hide();
		visualizarMeusAmigosPanel.hide();
	}

	private FormPanel criarPanel(){
		FormPanel panel = new FormPanel();
		panel.setBorders(false);
//		panel.setWidth(600);
		panel.setBodyBorder(false);
		panel.setLabelWidth(55);
		panel.setPadding(5);
		panel.setHeaderVisible(false);
		return panel;
	}

	private ContentPanel visualizarMeustIntesLayout(){

		BeanModelReader reader = new BeanModelReader();  

		DataProxy<Item> proxy = new DataProxy<Item>() {

			@Override
			public void load(DataReader<Item> reader, Object loadConfig,
					AsyncCallback<Item> callback) {
				// TODO Auto-generated method stub
				
			}
			
		};

		// loader and store  
		ListLoader<ListLoadResult<ModelData>> loader = new BaseListLoader<ListLoadResult<ModelData>>(proxy, reader);  
		ListStore<BeanModel> store = new ListStore<BeanModel>();  

		loader.load();  

		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();  
		columns.add(new ColumnConfig("name", "Nome", 100));  
		columns.add(new ColumnConfig("descricao", "Descricao", 100));  
		columns.add(new ColumnConfig("categoria", "Categoria", 100));  
		ColumnModel cm = new ColumnModel(columns);  

		Grid<BeanModel> grid = new Grid<BeanModel>(store, cm);
		grid.setAutoExpandColumn("name");  
		ContentPanel panel = new ContentPanel();  
		panel.setAutoHeight(true);
		panel.setAutoWidth(true);
		panel.setHeading("Meus Itens");  
		//panel.setSize(400, 200);  
		//		panel.setIcon(Resources.ICONS.table());  
		panel.setLayout(new FitLayout());  
		panel.add(grid);  
		return panel;
	}

	
	private ContentPanel visualizarAmigosLayout(){
			
		    ListStore<BeanModel> store = new ListStore<BeanModel>();  
		  
		    final PagingToolBar toolBar = new PagingToolBar(20);  
		  
		    List<ColumnConfig> columns = new ArrayList<ColumnConfig>();  
		    columns.add(new ColumnConfig("name", "Nome", 310));  
		    columns.add(new ColumnConfig("username", "Login", 310)); 
		    columns.add(new ColumnConfig("endereco", "Endereco", 320)); 
		    ColumnModel cm = new ColumnModel(columns);  
		  
		    Grid<BeanModel> grid = new Grid<BeanModel>(store, cm);  
		   
		    grid.setLoadMask(true);  
		    grid.setBorders(true);  
		    ContentPanel panel = new ContentPanel();  
		    panel.setFrame(true);  
		    panel.setCollapsible(true);  
		    panel.setAnimCollapse(false);   
		    panel.setHeading("Meus Amigos");  
		    panel.setLayout(new FitLayout());  
		    panel.add(grid); 
		    panel.setSize(950, 300);  
		    panel.setBottomComponent(toolBar);   
		    return panel; 
		  
		  }  
}  

